<?php
/**
 * 學期成績
 * @author cik
 *
 */
class Student_Model_StudentScoreSemester extends Student_Model_Doctrine_BaseStudentScoreSemester
{

    public function getClassScopeScore($yearSemmester,$classSn)
    {
          // 取系統預設值
        $param = new Default_Model_ModulesParam();
        $param_arr = $param->getParamByResourceId('score');
        // 小數計算位數
        $roundNum = $param_arr['round_number'] ;
        $finScoreRateMode  =  $param_arr['fin_score_rate_mode'];  //學期平均計算方式
        $arr = $resArr = array();

        $tempArr = explode("-", $this->getRequest()->getPost('yearSemester'));
        $year = (int) $tempArr[0];
        $semester = (int) $tempArr[1];
        $tempArr = explode("-", $this->getRequest()->getPost('select-class'));
        $grade = (int) $tempArr[0];
        $classSn = (int) $tempArr[1];

        // 取出學生成績
        $q = $this->getTable()->createQuery('a')
        ->select('a.score, b.year,b.semester, b.grade, b.scope, b.rate , c.judgement as judgement,
           d.name AS student_name, e.number AS student_number ')
        ->leftJoin('a.Semester_Model_SemesterViewCourseSubject b')
        ->leftJoin('Semester_Model_SemesterExamine c')
        ->leftJoin('a.Student_Model_Student d')
        ->leftJoin('d.Student_Model_StudentSemester e')
        ->andwhere('b.year=c.year')
        ->andWhere('b.semester=c.semester')
        ->andWHere('b.grade=c.grade')
        ->andWhere('e.year=?', $year)
        ->orderBy('b.rank');
        $res = $q->fetchArray();

        // 計算領域平均及等第
        foreach ($res as $row) {
            $subjectArr = $row['Semester_Model_SemesterViewCourseSubject'];
            $yearSemester = $subjectArr['year'].'-'.$subjectArr['semester'].'-'. $subjectArr['grade'];

            // 成績
            if (!isset($arr[$yearSemester] [$subjectArr['scope']]['score']))
            $arr[$yearSemester] [$subjectArr['scope']]['score'] = 0;
            // 領域成績加總
            if (!isset($arr[$yearSemester] [$subjectArr['scope']]['rate_total']))
            $arr[$yearSemester] [$subjectArr['scope']]['rate_total'] = 0;

            $arr[$yearSemester] [$subjectArr['scope']]['score'] +=
            $row['score'] * $subjectArr['rate'];

            $arr[$yearSemester] [$subjectArr['scope']]['rate_total'] +=
            $subjectArr['rate'];

            $arr[$yearSemester] [$subjectArr['scope']] ['judge'] = $row['judgement'] ;

        }


        // 計算平均及等第
        foreach ($arr as $yearSemester=>$row) {
            $tempAverage = array();
            foreach ($row as $scope=>$val) {
                $resArr[$yearSemester]['judge'] = $val['judge'];
                $score =  round($val['score'] / $val['rate_total'],$roundNum);
                $resArr[$yearSemester]['scope'][$scope]['score'] = $score;

                // 各學期平均
                if ($finScoreRateMode == 'arithmeticAverage')  {// 算術平均
                    $tempAverage['total'][] = 1;
                    $tempAverage['score'][] = $score;
                }
                else {
                    $tempAverage['total'][] = $val['rate_total'];
                    $tempAverage['score'][] = $score * $val['rate_total'];
                }
                $resArr[$yearSemester]['scope'][$scope]['rate'] = $val['rate_total'] ;
                $resArr[$yearSemester]['scope'][$scope]['judge'] =
                Semester_Model_SemesterExamine::resolveJudgement($resArr[$yearSemester]['scope'][$scope]['score'] ,
                $val['judge']);
            }
            // 計算各學期平均分數
            if ($tt = array_sum($tempAverage['total'])) {
                $ss = $resArr[$yearSemester]['average']['score'] = round(array_sum($tempAverage['score']) /$tt,$roundNum);
                $resArr[$yearSemester]['average']['judge'] =
                Semester_Model_SemesterExamine::resolveJudgement($ss ,$val['judge']);
            }

        }

        return $resArr;
    }

    /**
     *  列出學生領域成績
     * @param integer  $studentSn 學生流水號
     * @param string $yearSemester 學期 default null 代表所有學期
     * @return array
     */
    public function getStudentScopeScore($studentSn,$yearSemester = null)
    {
        // 取系統預設值
        $param = new Default_Model_ModulesParam();
        $param_arr = $param->getParamByResourceId('score');
        // 小數計算位數
        $roundNum = $param_arr['round_number'] ;
        $finScoreRateMode  =  $param_arr['fin_score_rate_mode'];  //學期平均計算方式
        $arr = $resArr = array();

        // 取出學生成績
        $q = $this->getTable()->createQuery('a')
        ->select('a.score, b.year,b.semester, b.grade, b.scope, b.rate , c.judgement as judgement')
        ->leftJoin('a.Semester_Model_SemesterViewCourseSubject b')
        ->leftJoin('Semester_Model_SemesterExamine c')
        ->where('a.student_sn=?', $studentSn)
        ->andwhere('b.year=c.year')
        ->andWhere('b.semester=c.semester')
        ->andWHere('b.grade=c.grade')
        ->orderBy('b.rank');
        $res = $q->fetchArray();

        // 計算領域平均及等第
        foreach ($res as $row) {
            $subjectArr = $row['Semester_Model_SemesterViewCourseSubject'];
            $yearSemester = $subjectArr['year'].'-'.$subjectArr['semester'].'-'. $subjectArr['grade'];

            // 成績
            if (!isset($arr[$yearSemester] [$subjectArr['scope']]['score']))
            $arr[$yearSemester] [$subjectArr['scope']]['score'] = 0;
            // 領域成績加總
            if (!isset($arr[$yearSemester] [$subjectArr['scope']]['rate_total']))
            $arr[$yearSemester] [$subjectArr['scope']]['rate_total'] = 0;

            $arr[$yearSemester] [$subjectArr['scope']]['score'] +=
            $row['score'] * $subjectArr['rate'];

            $arr[$yearSemester] [$subjectArr['scope']]['rate_total'] +=
            $subjectArr['rate'];

            $arr[$yearSemester] [$subjectArr['scope']] ['judge'] = $row['judgement'] ;

        }


        // 計算平均及等第
        foreach ($arr as $yearSemester=>$row) {
            $tempAverage = array();
            foreach ($row as $scope=>$val) {
                $resArr[$yearSemester]['judge'] = $val['judge'];
                $score =  round($val['score'] / $val['rate_total'],$roundNum);
                $resArr[$yearSemester]['scope'][$scope]['score'] = $score;

                // 各學期平均
                if ($finScoreRateMode == 'arithmeticAverage')  {// 算術平均
                    $tempAverage['total'][] = 1;
                    $tempAverage['score'][] = $score;
                }
                else {
                    $tempAverage['total'][] = $val['rate_total'];
                    $tempAverage['score'][] = $score * $val['rate_total'];
                }
                $resArr[$yearSemester]['scope'][$scope]['rate'] = $val['rate_total'] ;
                $resArr[$yearSemester]['scope'][$scope]['judge'] =
                Semester_Model_SemesterExamine::resolveJudgement($resArr[$yearSemester]['scope'][$scope]['score'] ,
                $val['judge']);
            }
            // 計算各學期平均分數
            if ($tt = array_sum($tempAverage['total'])) {
                $ss = $resArr[$yearSemester]['average']['score'] = round(array_sum($tempAverage['score']) /$tt,$roundNum);
                $resArr[$yearSemester]['average']['judge'] =
                Semester_Model_SemesterExamine::resolveJudgement($ss ,$val['judge']);
            }

        }

        return $resArr;
    }
}